package com.base.cn.platform.os.service.statistics.goods;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.statistics.goods.ZStatisticsSubjectStudyUVCountDao;
import com.base.cn.platform.os.entity.statistics.goods.ZStatisticsSubjectStudyUVCount;
import com.base.cn.platform.os.service.manage.subject.SysSubjectBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 专业每天学习UV统计Biz
 *
 * @author s.li
 * @date 2019/04/25
 */
@Service
public class ZStatisticsSubjectStudyUVCountBiz extends BaseBiz<ZStatisticsSubjectStudyUVCount, ZStatisticsSubjectStudyUVCountDao> {

    private static final String TIME_FORMAT = "yyyy-MM-dd";

    @Autowired
    private ZStatisticsSubjectStudyUVCountDao zStatisticsSubjectStudyUVCountDao;
    @Autowired
    private SysSubjectBiz sysSubjectBiz;

    /**
     * 生成专业学习UV统计记录
     * @param subjectStudyUVCountList
     */
    @Transactional
    public void genSubjectStudyUVCount(List<ZStatisticsSubjectStudyUVCount> subjectStudyUVCountList){
        Date nowDate = new Date();
        //获取列表中的时间条件
        String timeWhereSql = subjectStudyUVCountList.stream().map(e-> "countTime ='"+ DateUtils.format(e.getCountTime(), TIME_FORMAT)+"'").collect(Collectors.joining(" or "," 1=1 and (",")"));
        List<ZStatisticsSubjectStudyUVCount> uvCountList = this.find(timeWhereSql,null,null);
        List<ZStatisticsSubjectStudyUVCount> updateList = new ArrayList<>();
        List<ZStatisticsSubjectStudyUVCount> addList = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(uvCountList)){
            Map<String,ZStatisticsSubjectStudyUVCount> oldUVMap = uvCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(), TIME_FORMAT)+"_"+e.getSubjectIds(), e->e));
            Map<String,ZStatisticsSubjectStudyUVCount> newUVMap = subjectStudyUVCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(), TIME_FORMAT)+"_"+e.getSubjectIds(), e->e));
            newUVMap.forEach((k,v)->{
                ZStatisticsSubjectStudyUVCount oldData = oldUVMap.get(k);
                if(ObjectUtils.isNotEmpty(oldData)){
                    oldData.setUvCount(v.getUvCount());
                    updateList.add(oldData);
                }else{
                    addList.add(v);
                }
            });
        }else{
            addList.addAll(subjectStudyUVCountList);
        }
        if(ObjectUtils.isNotEmpty(addList)){
            this.batchSave(addList);
        }
        if(ObjectUtils.isNotEmpty(updateList)){
            this.updateBatch(updateList);
        }
    }

    /**
     * 学习榜 TOP7之 专业/科目
     * @param timeSql 时间条件
     * @return List<ZStatisticsSubjectStudyUVCount>
     */
    public List<ZStatisticsSubjectStudyUVCount> querySubjectStudyRankingList(String timeSql){
        List<ZStatisticsSubjectStudyUVCount> subjectStudyUVCountList = zStatisticsSubjectStudyUVCountDao.querySubjectStudyRankingList(ZStatisticsSubjectStudyUVCount.class,timeSql);
        //设置以专业数据
        if(ObjectUtils.isNotEmpty(subjectStudyUVCountList)){
            sysSubjectBiz.setSubjectData(subjectStudyUVCountList,true);
        }
        return subjectStudyUVCountList;
    }
}
